package cn.itcast.test;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

import static cn.itcast.n2.util.Sleeper.sleep;

@Slf4j(topic = "c.LockCas")
public class LockCas {

    //0 无锁  1 加锁
    private AtomicInteger state = new AtomicInteger(0);

    public void lock(){
        while (true){
            if (state.compareAndSet(0,1)) {
                break;
            }
        }
    }

    public void unlock(){
        log.debug("unlock...");
        state.set(0);
    }

    public static void main(String[] args) {
        final LockCas lock = new LockCas();
        new Thread(()->{
            log.debug("begin...");
            lock.lock();
            try {
                log.debug("lock...");
                sleep(1);
            }finally {
                lock.unlock();
            }
        },"t1").start();

        new Thread(()->{
            log.debug("begin...");
            lock.lock();
            try {
                log.debug("lock...");
            }finally {
                lock.unlock();
            }
        },"t2").start();
    }
}
